package com.bee.剑指offer;

import java.util.ArrayList;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2021-03-10
 */
public class 从尾到头打印链表 {

    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> tempArray = new ArrayList<Integer>();
        ArrayList<Integer> result = new ArrayList<Integer>();
        ListNode head = listNode;
        ListNode temp = listNode;
        while (temp != null){
            tempArray.add(temp.val);
            temp = temp.next;
        }
        for (int i = tempArray.size() -1; i >= 0 ; i--) {
            result.add(tempArray.get(i));
        }
        return result;
    }

    /**
     * 方法二：使用栈的形式，进行倒序输出链表
     */

    public static void main(String[] args) {
        ListNode listNode = new ListNode(67);
        listNode.next = new ListNode(0);
        listNode.next.next = new ListNode(24);
        listNode.next.next.next = new ListNode(58);
        ArrayList<Integer> array = printListFromTailToHead(listNode);
        System.out.println(array);
    }

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}


